#!/usr/bin/perl -w

#####################################################################
# Program Variables - Go see the central config.pm!
#####################################################################
use FindBin qw($Bin);
use lib "$Bin/..";
use config;
use layout;

#####################################################################
# Program Modules
#####################################################################
# CGI Modules
use CGI;
use CGI::Carp;
use CGI::Pretty;

# Database Module
use DBI;

#####################################################################
# Program Start
#####################################################################
my $cgi = new CGI;

my $graphname = $cgi->param('graph');

my @graphs;

my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file", "", "");

my $testref = $dbh->selectrow_hashref("SELECT * FROM tests WHERE name=?", undef, $graphname);
if (!defined($testref)) {
    &ts_pagestart("Invalid Graph");
    print $cgi->center("You have attempted to view an invalid graph.");
    print $cgi->p;
    print $cgi->center("Return to the " . $cgi->a({ -href => "./" }, "Index Page") . ".");
    print $cgi->end_html, "\n";
    exit 0;
}
$dbh->disconnect();

my %testhash  = %{$testref};
my $pagetitle = &read_config_option("tank_name") . " $testhash{title} Graphs";

push(@graphs, $cgi->td([ $cgi->img({ -src => "graphs/statistics-$graphname-day.png" }) . $cgi->br . "Last day" ]));
push(@graphs, $cgi->td([ $cgi->img({ -src => "graphs/statistics-$graphname-week.png" }) . $cgi->br . "Last week" ]));
push(@graphs, $cgi->td([ $cgi->img({ -src => "graphs/statistics-$graphname-month.png" }) . $cgi->br . "Last month" ]));
push(@graphs, $cgi->td([ $cgi->img({ -src => "graphs/statistics-$graphname-year.png" }) . $cgi->br . "Last year" ]));

# Web Page output

&ts_pagestart($pagetitle);
print $cgi->start_table(-align => "center");
print $cgi->Tr([@graphs]);
print $cgi->end_table;

print $cgi->p();
print $cgi->center("Return to the " . $cgi->a({ -href => "graphindex.cgi" }, &read_config_option("tank_name") . " Graph Index") . " Page.");
print $cgi->end_html;
print "\n";

# Okay, we're done, exit now.
exit 0;

